﻿using System;
using System.Collections.Generic;
using System.Text;
using Store.Model.Models;
using System.Linq;

namespace Store.Dal
{
    public class UsersDal
    {
        private readonly StoreContext db;
        public UsersDal(StoreContext user)
        {
            db = user;
        }
        //显示
        public List<User> GetUsers(string id, string name,int pageIndex,int pageSize,out int total)//条件查询
        {
            var str = from s in db.Users where s.UserDelState.Equals(1) select s;
            if (id != null)
            {
                int id1 = Convert.ToInt32(id);
                str = str.Where(s => s.UserId.Equals(id1));
            }
            if (name != null)
            {
                str = str.Where(s => s.UserName.Equals(name));
            }
            total = str.Count();
            var sql = str.OrderBy(s=>s.UserId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return sql;
        }
        //显示
        public List<User> GetUsers2(string userName, string userAccount,string userPrice,string userCount, string userAvg,int pageIndex, int pageSize, out int total)//条件查询
        {
            var str = from s in db.Users where s.UserDelState.Equals(1) select s;
            if (userName != null)
            { 
                str = str.Where(s => s.UserName.Equals(userName));
            }
            if (userAccount != null)
            {
                str = str.Where(s => s.UserAccount.Equals(userAccount));
            }
            if (userPrice != null)
            {
                var num = Convert.ToDouble(userPrice);
                str = str.Where(s => s.UserPrice.Equals(num));
            }
            if (userCount != null)
            {
                var num = Convert.ToInt32(userCount);
                str = str.Where(s => s.UserCount.Equals(num));
            }
            if (userAvg != null)
            {
                var num = Convert.ToDouble(userAvg);
                str = str.Where(s => s.UserAvg.Equals(userAvg));
            }
            total = str.Count();
            var sql = str.OrderBy(s => s.UserId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return sql;
        }
        //删除
        public int Delete(int id)
        {
            var str = db.Users.Find(id);
            if (str != null)
            {
                str.UserDelState = 0;
                return db.SaveChanges();
            }
            return -1;
        }
        //反填
        public User fantian(int id)
        {
            var str = from s in db.Users
                      where s.UserId.Equals(id)
                      select s;
            return str.FirstOrDefault();
        }
        //修改
        public int Update(User user)
        {
            var str = db.Users.Find(user.UserId);
            if (str != null)
            { 
                str.UserAccount = user.UserAccount;
                str.UserName = user.UserName;
                str.UserPhone = user.UserPhone;
                str.UserSex = user.UserSex;
                str.CreateTime = user.CreateTime;
                str.UserPwd = user.UserPwd;
                str.UserPrice = user.UserPrice;
                str.UserCount = user.UserCount;
                str.UserAvg = user.UserAvg;
                str.UserBirthday = user.UserBirthday;
                str.UserImg = user.UserImg;
                str.UserBuytime = user.UserBuytime;
                str.UserState = user.UserState;
                str.UserBalance = user.UserBalance; 
                return db.SaveChanges();
            }
            return -1;
        }
        //添加
        public int Add(User user)
        { 
            db.Users.Add(user); 
            return db.SaveChanges(); 
        }

    }
}
